#include "MinerWifeStates.hpp"

#include <iostream>

#include <SmegCore/Game/MessageDispatcher.hpp>

#include "MessageTypes.h"

//--------------------------------------------------------
void MinerWifeGlobalState::OnEnter( MinerWife* _pEntity ) {
}

void MinerWifeGlobalState::OnUpdate( MinerWife* _pEntity ) {
    if ( !_pEntity->GetStateMachine()->IsInState( VisitBathroom::GetInstance() ) &&
        rand() % 100 <= 10 ) {
        _pEntity->GetStateMachine()->ChangeState( VisitBathroom::GetInstance() );
    }
}

void MinerWifeGlobalState::OnExit( MinerWife* _pEntity ) {
}

bool MinerWifeGlobalState::HandleMessage( MinerWife* _pEntity, const smeg::core::MessageInfo& _msg ) {
    switch( _msg.m_iMessageType ) {
    case eMsg_HoneyImHome:
        _pEntity->GetStateMachine()->ChangeState( CookStew::GetInstance() );

        smeg::core::MessageDispatcher::GetInstance()->DispatchMessage(
            2.5f,
            _pEntity,
            _pEntity,
            eMsg_StewReady,
            NO_EXTRA_INFO );

        return true;

    case eMsg_StewReady:
        _pEntity->GetStateMachine()->ChangeState( DoHouseWork::GetInstance() );

        smeg::core::MessageDispatcher::GetInstance()->DispatchMessage(
            MSG_SEND_IMMEDIATELY,
            _pEntity,
            _pEntity->GetHusband(),
            eMsg_StewReady,
            NO_EXTRA_INFO );

        return true;
    }

    return false;
}

//--------------------------------------------------------
void DoHouseWork::OnEnter( MinerWife* _pEntity ) {
    std::cout << "--- MinerWife starting housework!" << std::endl;
}

void DoHouseWork::OnUpdate( MinerWife* _pEntity ) {
    
    std::cout << "LaLiLaLiLa housework..." << std::endl;
}

void DoHouseWork::OnExit( MinerWife* _pEntity ) {
    std::cout << "--- MinerWife stop housework!" << std::endl;
}

//--------------------------------------------------------
void VisitBathroom::OnEnter( MinerWife* _pEntity ) {
    std::cout << "--- MinerWife going to the bathroom!" << std::endl;
}

void VisitBathroom::OnUpdate( MinerWife* _pEntity ) {
    std::cout << "Don't look in there!" << std::endl;
    _pEntity->GetStateMachine()->RevertToPreviousState();
}

void VisitBathroom::OnExit( MinerWife* _pEntity ) {
    std::cout << "--- MinerWife leaving the bathroom!" << std::endl;
}

//--------------------------------------------------------
void CookStew::OnEnter( MinerWife* _pEntity ) {
    std::cout << "--- Need to cook a good stew for my husband!" << std::endl;
}

void CookStew::OnUpdate( MinerWife* _pEntity ) {
    std::cout << "Cooking somme good stew..." << std::endl;
}

void CookStew::OnExit( MinerWife* _pEntity ) {
    std::cout << "--- Stew if ready!" << std::endl;
}
